/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.teach.service;

import java.util.List;

import com.google.common.collect.Lists;
import com.lczyfz.istep.common.constants.DeleteFlag;
import com.lczyfz.istep.common.constants.TeacherRole;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.cms.entity.Link;
import com.lczyfz.istep.modules.sys.dao.RoleDao;
import com.lczyfz.istep.modules.sys.service.SystemService;
import com.lczyfz.istep.modules.teach.dao.TeachStudentDao;
import com.lczyfz.istep.modules.teach.entity.TeachStudent;
import com.lczyfz.istep.modules.teach.entity.TeachTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.service.CrudService;
import com.lczyfz.istep.modules.teach.entity.TeachClass;
import com.lczyfz.istep.modules.teach.dao.TeachClassDao;

/**
 * 班级管理Service
 * @author cqh
 * @version 2017-01-12
 */
@Service
@Transactional(readOnly = true)
public class TeachClassService extends CrudService<TeachClassDao, TeachClass> {
	@Autowired
	TeachStudentDao teachStudentDao ;
	@Autowired
	SystemService systemService;
	@Autowired
	RoleDao roleDao;
	@Autowired
	TeachClassDao teachClassDao ;



	public TeachClass get(String id) {
		return super.get(id);
	}
	
	public List<TeachClass> findList(TeachClass teachClass) {
		return super.findList(teachClass);
	}
	
	public Page<TeachClass> findPage(Page<TeachClass> page, TeachClass teachClass) {
		return super.findPage(page, teachClass);
	}


	@Transactional(readOnly = false)
	public void save(TeachClass teachClass) {
		super.save(teachClass);
	}
	
//	@Transactional(readOnly = false)
//	public void delete(TeachClass teachClass) {
//		super.delete(teachClass);
//	}
	@Transactional(readOnly = false)
	public void delete(TeachClass teachClass) {
		teachClassDao.deleteClassStudentModify(teachClass.getId());
		super.delete(teachClass);
	}



	//update ?



	//	AlisaYe
	// 2017/02/28
	//添加、修改学生(?)
	@Transactional(readOnly = false)
	public void saveClass(TeachClass teachClass){
		if(StringUtils.isBlank(teachClass.getId())){
			teachClass.preInsert();
			teachClassDao.insert(teachClass);
			logger.debug("==AlisaYe====insert==teach_class==s="+teachClass.getUserId()+","+ teachClass.getClassName()+","+teachClass.getRemarks());
			//插入用户和角色关联表数据
			teachClassDao.teacherInsertUserRole(teachClass.getUserId(), systemService.getRoleByEnname(TeacherRole.teacher).getId());
		}else {
			teachClass.preUpdate();
			teachClassDao.update(teachClass);
		}
	}


	@Transactional(readOnly = false)
	public void insertTeachClassStudent(String id,String userId){
			 teachClassDao.insertTeachClassStudent(id,userId);
	}
	@Transactional(readOnly = false)
	public void deleteClassStudentModify(String id){
		teachClassDao.deleteClassStudentModify(id);
	}





	//	AlisaYe
//	public List<TeachStudent> findAllUserList(){return teachClassDao.findAllUserList();}
	public List<TeachTeacher> findAllTeacherList(){
		return teachClassDao.findAllTeacherList(DeleteFlag.deleteFlag);
	};



	//获取所有班级名
	public List<TeachClass> findAllClassName(){
		return teachClassDao.findAllClassName(DeleteFlag.deleteFlag);
	}

	//根据班级名获取班级记录
	public TeachClass findClassByClassName(String className) {
		return teachClassDao.findClassByClassName(className, DeleteFlag.deleteFlag);
	}

	//根据学生id找到所属的多个班级
	public List<TeachClass> findBelongClassById(String userId){
		return teachClassDao.findBelongClassById(userId, DeleteFlag.deleteFlag);
	}

	//根据班级id找到所属的学生
	public List<TeachStudent> findAllStudentByClassId(String id){
		return teachClassDao.findAllStudentByClassId(id, DeleteFlag.deleteFlag);
	}


	public List<TeachTeacher> allOtherAddStudentList() {
		return teachClassDao.allOtherAddStudentList(DeleteFlag.deleteFlag);
	}
	public List<TeachStudent> studentClassIDonDeleteClassList() {
		return teachClassDao.studentClassIDonDeleteClassList(DeleteFlag.deleteFlag);
	}


	/**
	 *  @ AlisaYe
	 * 通过编号获取班级名称
	 */
//	public List<Object[]> findByIds0(String ids) {
//		List<Object[]> list = Lists.newArrayList();
//		String[] idss = StringUtils.split(ids, ",");
//		if (idss.length > 0) {
//			List<TeachClass> l = dao.findByIdIn0(idss);
//			for (TeachClass e : l) {
//				list.add(new Object[]{e.getId(), StringUtils.abbr(e.getClassName(), 64)});
//			}
//		}
//		return list;
//	}
//
//





}